function Sx = V2Charge(Se1,Se2,E,Vx)
% Note here that input Vx is actually Potential + Ef

% Energy grid setup
he = E(2)-E(1);
Emin = E(1); Emax = E(end);
Ne = length(E); Nx = length(Vx);
Sx = zeros(1,Nx);

% Finding indices of Vx on the energy grid
Vindex = round((Vx-Emin)/he)+1;

% % Assigning charge according to the indices
% Sx(1:Nx/2) = Se1(Vindex(1:Nx/2));
% Sx(Nx/2+1:end) = Se2(Vindex(Nx/2+1:end));

% Assigning charge according to the indices, with linear interpolation
% Material 1
Sn = Se1(Vindex(1:Nx/2)); Sn1 = Se1(1+Vindex(1:Nx/2));
Vn = E(Vindex(1:Nx/2)); Vn1 = E(1+Vindex(1:Nx/2));
Sx(1:Nx/2) = Sn + (Vx(1:Nx/2)-Vn).*(Sn1 - Sn)./(Vn1 - Vn);
% Material 2
Sn = Se2(Vindex(Nx/2+1:end)); Sn1 = Se2(1+Vindex(Nx/2+1:end));
Vn = E(Vindex(Nx/2+1:end)); Vn1 = E(1+Vindex(Nx/2+1:end));
Sx(Nx/2+1:end) = Sn + (Vx(Nx/2+1:end)-Vn).*(Sn1 - Sn)./(Vn1 - Vn);

end